﻿using B.S.DataBase.MyProject.Domain;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure.Interface;
using B.S.DataBase.MyRead.API.Appicant.Command.RoleMenu;
using B.S.DataBase.MyRead.API.DTOs;
using MediatR;

namespace B.S.DataBase.MyRead.API.Appicant.CommandHandler.RoleMenu
{

    public class FTRoleMenuQueryCommandHandler : IRequestHandler<FTRoleMenuQueryCommand, APIResult<List<MenuByRoleDto>>>
    {
        private readonly IBaseRepository<Role> rRepo;
        private readonly IBaseRepository<RoleMenuModel> rmRepo;
        private readonly IBaseRepository<Menu> mRepo;

        public FTRoleMenuQueryCommandHandler(IBaseRepository<Role>rRepo,IBaseRepository<RoleMenuModel>rmRepo,IBaseRepository<Menu>mRepo)
        {
            this.rRepo = rRepo;
            this.rmRepo = rmRepo;
            this.mRepo = mRepo;
        }

        public Task<APIResult<List<MenuByRoleDto>>> Handle(FTRoleMenuQueryCommand request, CancellationToken cancellationToken)
        {
            APIResult<List<MenuByRoleDto>> result = new APIResult<List<MenuByRoleDto>>();
            result.Code = APIEnum.查询成功;
            result.Msg = APIEnum.查询成功.ToString();

            var list = from a in rRepo.Getall()
                       join b in rmRepo.Getall() on a.RoleId equals b.RoleId
                       join c in mRepo.Getall() on b.MenuId equals c.MenuId
                       select new MenuByRoleDto
                       {
                           RoleId = a.RoleId,
                           MenuId = c.MenuId,   
                           RoleName = a.RoleName,
                           MenuName = c.MenuName,
                           RMId = b.RMId,
                       };

            list = list.Where(x => x.RoleId == request.RoleId);
            result.data = list.ToList();
            return Task.FromResult(result); 



        }






    }
}
